package cn.itsource.pethome.department.controller;

import cn.itsource.pethome.department.domain.Department;
import cn.itsource.pethome.department.query.DepartmentQuery;
import cn.itsource.pethome.department.service.IDepartmentService;
import cn.itsource.pethome.util.AjaxResult;
import cn.itsource.pethome.util.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @GetMapping("/list")
    @ApiOperation(value = "通过全部查询" )
    public List<Department> queryAll(){
        return departmentService.queryAll();
    }

    /**
     * get 请求是没有请求的体 RequestBody
     * 400错 请求参数类型不匹配
     * @param
     * @return
     */
    @PostMapping("/pagelist")
    @ApiOperation(value = "高级查询&分页" )
    public PageList<Department> pageQueryAll(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.pageQueryAll(departmentQuery);
    }

    @GetMapping("/list/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department queryOne(@PathVariable("id") Long id){
        return departmentService.queryOne(id);
    }

    @PostMapping("/department")
    @ApiOperation(value = "添加 和更改 部门" )
    public AjaxResult insert(@RequestBody Department department){
        try {
            if(department.getId() == null){
                departmentService.insert(department);
            }else {
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @PutMapping("/department")
    @ApiOperation(value = "修改部门" )
    public AjaxResult update(@RequestBody Department department){
        try {
            departmentService.update(department);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @DeleteMapping("/department/{id}")
    @ApiOperation(value = "根据id删除部门" )
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @PatchMapping("/department")
    @ApiOperation(value = "批量删除部门" )
    public AjaxResult delete(@RequestBody List<Long> ids){
        try {
            departmentService.deleteAll(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @GetMapping("/tree")
    @ApiOperation(value = "无限极部门树" )
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }
}
